<template>
  <div class="box">
    <el-dialog title="关联权限" :visible.sync="closeBox" width="50%">
      <el-tree
        v-if="closeBox"
        ref="tree"
        :data="allAuthList"
        :props="{label:'name'}"
        node-key="id"
        :default-checked-keys="checkAuth"
        :check-strictly="true"
        show-checkbox
        default-expand-all
      />
      <span slot="footer">
        <el-button size="small" @click="closeBox = false">取消</el-button>
        <el-button size="small" type="primary" @click="btnOk">确定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { getPermissionListApi } from '@/api/permission'
import { transListToTreeData } from '@/utils'
import { assignPerm } from '@/api/role'

export default {
  name: 'SetAuth',
  data() {
    return {
      closeBox: false,
      allAuthList: [], // 所有的权限
      checkAuth: [], // 默认选中的权限
      roleId: ''
    }
  },
  created() {
    this.getAllAuth()
  },
  methods: {
    // 获取所有权限
    async getAllAuth() {
      const res = await getPermissionListApi()
      // 工具函数将数据城树状
      this.allAuthList = transListToTreeData(res, 0)
    },
    // 角色关联权限提交
    async btnOk() {
      await assignPerm({
        id: this.roleId,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.$message.success('操作成功')
      this.closeBox = false
      this.checkAuth = []
    }
  }
}
</script>

<style>

</style>
